package com.ant.storage.service.impl;

import com.ant.storage.common.ResultVo;
import com.ant.storage.mapper.StoragePaymentMapper;
import com.ant.storage.pojo.StorageContract;
import com.ant.storage.pojo.StoragePayment;
import com.ant.storage.pojo.UserInfo;
import com.ant.storage.service.IStoragePaymentService;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletContext;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hzd
 * @since 2024-06-14
 */
@Service
public class StoragePaymentServiceImpl extends ServiceImpl<StoragePaymentMapper, StoragePayment> implements IStoragePaymentService {

    @Autowired
    public ServletContext servletContext;


    @Autowired
    public StorageContractServiceImpl storageContractService;


    @Override
    public ResultVo paymentSave(StoragePayment dto) {
        synchronized (this) {
            UserInfo userInfo = (UserInfo) servletContext.getAttribute("userInfo");
            if (dto.getId() == null) {
                dto.setCreationBy(userInfo.getNameId().toString());
                dto.setCreationTime(new Date());
                this.baseMapper.insert(dto);
                storageContractService.update(new UpdateWrapper<StorageContract>()
                        .set("collection_date", dto.getDeadlineTime())
                        .eq("id", dto.getParentId()));
            } else {
                this.baseMapper.updateById(dto);
            }
            return ResultVo.success("操作成功");
        }
    }
}
